﻿using EmployeeLeaveSystem.Shared.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace EmployeeLeaveSystem.Server.Controllers.SalaryManager {
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class SalaryController : ControllerBase {
        readonly EpMainContext _context;
        public SalaryController(EpMainContext context) {
            _context = context;
        }
        // 获得规则指定（部门ID）
        public ActionResult<NowRules?> GetRulesByDepartmentId(int id) {
            return _context.NowRules.Include(x => x.Staff).Where(x => x.DepartmentId == id).OrderByDescending(x => x.Id).FirstOrDefault();
        }
        /// <summary>
        /// 获得特定月份的 出勤 历史记录
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult<List<AddWork>> GetAddWorkInfoByUsrIdAndMonth(int usrId, int month) {
            return _context.AddWorks.Include(x => x.AcceptStaff).Where(x => x.StaffId == usrId & x.StartTime.Month == month).ToList();
        }
        /// <summary>
        /// 获得特定月份的 加班 历史记录
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult<List<AddWOM>> GetAddWOMInfoByUsrIdAndMonth(int usrId, int month) {
            return _context.AddWOMs.Include(x => x.AcceptStaff).Where(x => x.StaffId == usrId & x.StartTime.Month == month).ToList();
        }
        //查询特定ID的薪水历史记录
        [HttpGet]
        public ActionResult<SalaryInfo> GetInfoById(int id) {
            return _context.SalaryInfos.Where(x => x.Id == id).Include(x => x.Staff).ThenInclude(x => x.UpperStaff).First();
        }
        [HttpGet]
        public ActionResult<List<SalaryInfo>> GetInfosByUsrId(int usrId) {
            return _context.SalaryInfos.Where(x => x.StaffId.Equals(usrId)).ToList();
        }
        // 查询所有的本部门薪水
        [HttpGet]
        public ActionResult<List<NowRules>> GetAllInfosByDepartmentId(int id) {
            return _context.NowRules.Where(x => x.DepartmentId == id).Include(x => x.Staff).ToList();
        }
        // 更新薪水规则记录，通过ID
        [HttpPost]
        public ActionResult UpdateRules(NowRules nowRules) {
            nowRules.Created = DateTime.Now;
            nowRules.Department = null;
            _context.NowRules.Update(nowRules);
            _context.SaveChanges();
            return Ok();
        }
        [HttpGet]
        public ActionResult DeleteRules(int id) {
            var enty = _context.NowRules.Where(y => y.Id == id).FirstOrDefault();
            _context.NowRules.Remove(enty);
            _context.SaveChanges();
            return Ok();
        }
        [HttpPost]
        public ActionResult<NowRules> AddRules(NowRules nowRules) {
            var res = _context.NowRules.Add(nowRules);
            _context.SaveChanges();
            return Ok();
        }
        /// <summary>
        /// 根据管理员id获得他所管辖的薪水审批记录
        /// </summary>
        [HttpGet]
        public ActionResult<List<SalaryInfo>> GetSalaryInfosManagerId(int id) {
            List<int> staffInfos = _context.StaffInfos.Where(x => x.UpperStaffId == id).Select(x => x.Id).ToList();
            return Ok(_context.SalaryInfos.Where(x => staffInfos.Contains(x.StaffId)).Include(x => x.Staff).ToList());
        }
        [HttpPost]
        public ActionResult UpdateSalaryInfos(List<SalaryInfo> salaryInfos) {
            foreach (var item in salaryInfos) {
                item.Staff = null;
            }
            _context.SalaryInfos.UpdateRange(salaryInfos);
            _context.SaveChanges();
            return Ok();
        }
        [HttpPost]
        public ActionResult AddSalaryInfo(SalaryInfo salaryInfo) {
            _context.Add(salaryInfo);
            _context.SaveChanges();
            return Ok();
        }
    }
}
